package com.rockcent.helper;

import com.rockcent.mall.lib.domain.CustomerFund;
import com.rockcent.mall.lib.domain.CustomerFundFlow;
import com.rockcent.mall.lib.domain.MallOrder;
import com.rockcent.mall.lib.domain.enumclass.CustomerFundFlowTransactionType;
import com.rockcent.mall.lib.domain.enumclass.FundTransactionFlow;

import java.math.BigDecimal;

/**
 * Created by oyhk on 2016/11/25.
 */
public enum CustomerFundFlowHelper {
    SINGLE;

    /**
     * 用户资金
     *
     * @param customerFund
     * @param mallId
     * @param orderNum
     * @param changeAvailableAmount
     * @param changeFrozen
     * @param operationAmount
     * @param transactionFlow
     * @param customerFundFlowTransactionType
     * @return
     */
    public CustomerFundFlow create(CustomerFund customerFund, Long mallId, String orderNum, BigDecimal changeAvailableAmount, BigDecimal changeFrozen, BigDecimal operationAmount, FundTransactionFlow transactionFlow, CustomerFundFlowTransactionType customerFundFlowTransactionType) {
        CustomerFundFlow customerFundFlow = new CustomerFundFlow();

        BigDecimal availableAmount = customerFund.getAvailableAmount();
        availableAmount = null == availableAmount ? BigDecimal.valueOf(0) : availableAmount;
        if (transactionFlow == FundTransactionFlow.INCOME) {
            customerFundFlow.setAvailableAmount(availableAmount.add(changeAvailableAmount));
            customerFundFlow.setFrozen(customerFund.getFrozen().add(changeFrozen));
        }
        if (transactionFlow == FundTransactionFlow.EXPEND) {
            customerFundFlow.setAvailableAmount(availableAmount.subtract(changeAvailableAmount));
            customerFundFlow.setFrozen(customerFund.getFrozen().subtract(changeFrozen));
        }

        customerFundFlow.setTotalAmount(customerFundFlow.getAvailableAmount().add(customerFundFlow.getFrozen()));
        customerFundFlow.setCustomerFundId(customerFund.getId());
        customerFundFlow.setCustomerId(customerFund.getCustomerId());
        customerFundFlow.setOperationAmount(operationAmount);
        customerFundFlow.setMallId(mallId);
        customerFundFlow.setTransactionFlow(transactionFlow);
        customerFundFlow.setTransactionType(customerFundFlowTransactionType);
        customerFundFlow.setRemark(String.format("%s-%s%s元", customerFundFlowTransactionType.getValue(), transactionFlow.getValue(), operationAmount.setScale(2, BigDecimal.ROUND_HALF_UP).toString()));
        customerFundFlow.setMallOrderNum(orderNum);
        return customerFundFlow;
    }
}
